package uit.nfcmenu.models;

import java.util.ArrayList;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class MenuAdapter extends DatabaseAdapter {

	public MenuAdapter(Context context) {
		super(context);
	}

	public int insertMenuModel(MenuModel model) {
		int value = -1;

		this.open();

		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();

			contentValues.put(DbDefines.TableMenu.menuID, model.getMenuID());
			contentValues
					.put(DbDefines.TableMenu.menuName, model.getMenuName());
			contentValues.put(DbDefines.TableMenu.subMenuID,
					model.getSubMenuID());
			contentValues.put(DbDefines.TableMenu.imageLink,
					model.getImageLink());
			contentValues.put(DbDefines.TableMenu.description,
					model.getDescription());

			// Add to database
			value = (int) this.mDb.insert(DbDefines.MenuModel, "",
					contentValues);
		} finally {
			this.close();
		}

		return value;
	}

	public int deleteMenuModel(int menuID) {
		int result = 0;
		this.open();
		try {
			result = this.mDb.delete(DbDefines.MenuModel,
					DbDefines.TableMenu.menuID + " = " + menuID, null);
		} finally {
			this.close();
		}
		return result;
	}

	public int updateMenuModel(MenuModel model) {
		int value = -1;
		this.open();
		try {
			// Assign values to add...
			ContentValues contentValues = new ContentValues();
			contentValues.put(DbDefines.TableMenu.menuID, model.getMenuID());
			contentValues
					.put(DbDefines.TableMenu.menuName, model.getMenuName());
			contentValues.put(DbDefines.TableMenu.subMenuID,
					model.getSubMenuID());
			contentValues.put(DbDefines.TableMenu.imageLink,
					model.getImageLink());
			contentValues.put(DbDefines.TableMenu.description,
					model.getDescription());
			// Add to database
			value = (int) this.mDb.update(DbDefines.MenuModel, contentValues,
					DbDefines.TableMenu.menuID + " = " + model.getMenuID(),
					null);
			if (value == 0) {
				this.close();
				return insertMenuModel(model);
			}
		} finally {
			this.close();
		}
		return value;
	}

	public MenuModel loadMenuModel(int menuID) {
		this.open();
		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		try {
			cursor = this.mDb.query(DbDefines.MenuModel, new String[] {
					DbDefines.TableMenu.menuID, DbDefines.TableMenu.menuName,
					DbDefines.TableMenu.subMenuID,
					DbDefines.TableMenu.imageLink,
					DbDefines.TableMenu.description },
					DbDefines.TableMenu.menuID + " = " + "'" + menuID + "'",
					null, "", "", "", "1");

			if (cursor != null && cursor.getCount() > 0) {
				cursor.moveToFirst();
				model = new MenuModel(cursor);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return model;
	}

	public ArrayList<MenuModel> getMainMenus() {
		// TODO Auto-generated method stub

		this.open();

		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		ArrayList<MenuModel> menus = new ArrayList<MenuModel>();
		try {
			cursor = this.mDb.query(DbDefines.MenuModel, new String[] {
					DbDefines.TableMenu.menuID, DbDefines.TableMenu.menuName,
					DbDefines.TableMenu.subMenuID,
					DbDefines.TableMenu.imageLink,
					DbDefines.TableMenu.description },
					DbDefines.TableMenu.subMenuID + " = " + "'" + 0 + "'",
					null, "", "", "", "");

			while (cursor.moveToNext()) {
				model = new MenuModel(cursor);
				menus.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return menus;
	}

	public ArrayList<MenuModel> getSubMenu(int mainId) {
		// TODO Auto-generated method stub

		this.open();

		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		ArrayList<MenuModel> menus = new ArrayList<MenuModel>();
		try {
			cursor = this.mDb.query(DbDefines.MenuModel, new String[] {
					DbDefines.TableMenu.menuID, DbDefines.TableMenu.menuName,
					DbDefines.TableMenu.subMenuID,
					DbDefines.TableMenu.imageLink,
					DbDefines.TableMenu.description },
					DbDefines.TableMenu.subMenuID + " = " + "'" + mainId + "'",
					null, "", "", "", "");

			while (cursor.moveToNext()) {
				model = new MenuModel(cursor);
				menus.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return menus;
	}

	public ArrayList<MenuModel> selectAll() {
		// TODO Auto-generated method stub

		this.open();

		// getting data from database
		Cursor cursor = null;
		MenuModel model = null;
		ArrayList<MenuModel> menus = new ArrayList<MenuModel>();
		try {
			cursor = mDb.rawQuery("SELECT * FROM " + DbDefines.MenuModel, null);

			while (cursor.moveToNext()) {
				model = new MenuModel(cursor);
				menus.add(model);
			}
		} finally {
			if (cursor != null) {
				cursor.close();
			}
			this.close();
		}
		return menus;
	}
}
